package com.sig.xml;

import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import android.util.Log;
import utils.Graphe;
import utils.Point;
import utils.Polygon;
import utils.Segment;

public class MapXmlParser extends XmlParser{

	public MapXmlParser(String xml) {
		super(xml);
	}
	public MapXmlParser(XmlPullParser xpp) {
		super(xpp);
	}

	public static final String POLYGONE = "polygone";
	public static final String CHEMIN = "chemin";
	public static final String POINT = "point";   
	public static final String SEGMENT = "segment";
	public static boolean parseBatiment = false;
	public Graphe getMap() {
		int eventType = XMLgetEventType(); 
		List<Polygon> polygones = new ArrayList<Polygon>();
		List<Segment> segments = new ArrayList<Segment>();
		int count = 0;
		do{
			if (eventType == XmlPullParser.START_TAG) {
				String tag = getXpp().getName();
				if(tag.equals(POLYGONE)){
					Polygon polygone = getPolygone();       
					polygones.add(polygone);
				}

				else if(tag.equals(CHEMIN)){
					Segment chemin = getChemin(); 
					if(chemin.getId().equals("1107863449_3")){
						Log.e("Chemin",chemin.getDebut().toString());
						Log.e("Connard",tag +""  + count);
					}
					if(chemin.getId().equals("477549410_01")){
						Log.e("Chemin",chemin.toString());
					}
					segments.add(chemin);
				}
			}
			count++;
			eventType = XMLgetSuivant();
		}while (eventType != XmlPullParser.END_DOCUMENT);

		return new Graphe(polygones, segments);
	}

	public Polygon getPolygone(){

		Polygon polygone = new Polygon();

		for(int i=0;i<getXpp().getAttributeCount();++i){
			String attrName = getXpp().getAttributeName(i);
			String attrValue = getXpp().getAttributeValue(i);
			if(attrName.equals("id")){
				String id = attrValue;
				polygone.setId(id);
			}else if(attrName.equals("type")){
				String type = attrValue;
				polygone.setType(type);
			}
			else if(attrName.equals("nom")){
				String nom = attrValue;
				polygone.setNom(nom);
			}
		}

		int eventType = XMLgetSuivant();
		while (eventType != XmlPullParser.END_TAG) { 
			if (eventType == XmlPullParser.START_TAG) {
				String tag = getXpp().getName();

				if(tag.equals(POINT)){
					polygone.addPoint(getPoint());
				}else if(tag.equals(CHEMIN)){
					//Log.e("Je rentre","ajout chemin");
					String id = getXpp().getAttributeValue(null,"id");
					polygone.getCheminSortie().add(id);		
					XMLgetSuivant();

				}

			}
			eventType = XMLgetSuivant();
		} 
		return polygone;
	}

	public Segment getChemin(){
		Segment chemin = new Segment();

		for(int i=0;i<getXpp().getAttributeCount();++i){
			String attrName = getXpp().getAttributeName(i);
			String attrValue = getXpp().getAttributeValue(i);
			if(attrName.equals("id")){
				String id = attrValue;
				chemin.setId(id);
			}else if(attrName.equals("type")){
				String type = attrValue;
				chemin.setType(type);
			}
		}

		 int eventType = XMLgetSuivant();

		do{

			if (eventType == XmlPullParser.START_TAG) {
				String tag = getXpp().getName();
				

				if(tag.equals(POINT)){
					Point p = getPoint();
					chemin.addPoint(p);					
				}
				else if(tag.equals(POLYGONE)){
				
					String id = getXpp().getAttributeValue(null,"id");
					chemin.getPolygones().add(id);					
					getString();

				}
				else if(tag.equals(SEGMENT)){

					String id = getXpp().getAttributeValue(null,"id");	
					chemin.getCroisements().add(id);
					getString();
				}
			}
			eventType = XMLgetSuivant();
		}while (eventType != XmlPullParser.END_TAG);

		return chemin;
	}

	public static String POINT_X = "x";
	public static String POINT_Y = "y";

	public Point getPoint(){
		Point point = new Point();

		int eventType = XMLgetEventType(); 
		do { 
			if (eventType == XmlPullParser.START_TAG) {
				Float x = Float.parseFloat(getXpp().getAttributeValue(null,POINT_X));
				Float y = Float.parseFloat(getXpp().getAttributeValue(null,POINT_Y));

				if(parseBatiment){
				point.setX(x*1000);
				point.setY(y*1000);
				}else{
					point.setX(x);
					point.setY(y);
				}
			}
			eventType = XMLgetSuivant();
		}while (eventType != XmlPullParser.END_TAG);


		return point;
	}

}